package com.qixin.part.serviceimpl;

import com.qixin.part.bean.PtDic;
import com.qixin.part.bean.ZtreeBean;
import com.qixin.part.mapper.PtDicMapper;
import com.qixin.part.service.PtDicService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2018-08-20
 */
@Service
public class PtDicServiceImpl extends ServiceImpl<PtDicMapper, PtDic> implements PtDicService {

    @Override
    //自动缓存  当方法第一次执行的时候 会先去缓存中查一遍有没有这个缓存
    //有这个缓存 则 直接从缓存中返回出数据  方法不会再继续执行
    //没有这个缓存  则 执行方法
    @Cacheable(value ="dicData",key = "'dicData'")
    public List<ZtreeBean> selectDic() {
        return baseMapper.selectDic();
    }

    @Override
    @Transactional
    @CacheEvict(value ="dicData",key = "'dicData'")
    public boolean deletAllDicById(PtDic ptDic) {
        //删除
        if (baseMapper.deleteById(ptDic.getId())>0) {
            Map map = new HashMap<>();
            map.put("parent_id", ptDic.getId());
            baseMapper.deleteByMap(map);

            return true;

        }

        return false;
    }

    @Override
    public PtDic selectDicById(long id) {
        return baseMapper.selectDicById(id);
    }

    @Override

    public List<PtDic> findDataByDicCode(String code) {
        return baseMapper.findDataByDicCode(code);
    }

    @Override
    //干掉dicData缓存
    @CacheEvict(value = "dicData",key = "'dicData'")
    public boolean MyInsert(PtDic ptDic) {
        return this.insert(ptDic);
    }
}
